//
//  NSString+StringJudge.m
//  FadeSDWebImage
//
//  Created by 冷悦 on 16/2/25.
//  Copyright © 2016年 冷悦. All rights reserved.
//


#import "NSString+Validate.h"

@implementation NSString (Validate)

- (BOOL)validateRange:(NSArray *)termStrings Password:(NSString *)password{
    for (NSString *termString in termStrings) {
        NSRange range = [password rangeOfString:termString];
        
        if (range.location != NSNotFound) {
            return YES;
        }
    }
    
    return NO;
}

- (PasswordStrength)validatePasswordStrenth{
    NSArray *termStrings1 = @[@"a", @"b", @"c", @"d", @"e", @"f",
                             @"g", @"h", @"i", @"j", @"k", @"l",
                             @"m", @"n", @"o", @"p", @"q", @"r",
                             @"s", @"t", @"u", @"v", @"w", @"x",
                              @"y", @"z"];
    
    NSArray *termStrings2 = @[@"1", @"2", @"3", @"4", @"5", @"6",
                             @"7", @"8", @"9", @"0"];
    
    NSArray *termStrings3 = @[@"A", @"B", @"C", @"D", @"E", @"F",
                             @"G", @"H", @"I", @"J", @"K", @"L",
                             @"M", @"N", @"O", @"P", @"Q", @"R",
                             @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
    
    NSArray *termStrings4 = @[@"~",@"`",@"@",@"#",@"$",@"%",@"^",
                              @"&",@"*",@"(",@")",@"-",@"_",@"+",
                              @"=",@"{",@"}",@"[",@"]",@"|",@":",
                              @";",@"“",@"'",@"‘",@"<",@",",@".",
                              @">",@"?",@"/",@"、"];
    
    BOOL result1 = [self validateRange:termStrings1 Password:self];
    BOOL result2 = [self validateRange:termStrings2 Password:self];
    BOOL result3 = [self validateRange:termStrings3 Password:self];
    BOOL result4 = [self validateRange:termStrings4 Password:self];
    NSArray *results = @[@(result1), @(result2), @(result3), @(result4)];
    int resultCount = 0;
    
    for (NSNumber *boolNum in results) {
        resultCount = [boolNum boolValue] ? resultCount + 1 : resultCount;
    }
    
    if (self.length < 6) {
        return PasswordStrengthInvalid;
    } else if (resultCount < 2) {
        return PasswordStrengthLow;
    } else if (self.length >= 6 && resultCount == 2) {
        return PasswordStrengthNormal;
    } else if (self.length >= 6 && resultCount > 2) {
        return PasswordStrengthHigh;
    } else {
        return PasswordStrengthInvalid;
    }
}

- (BOOL)validatePhoneNumber:(BOOL)mobileOnly{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,1705
     * 联通：130,131,132,152,155,156,185,186,1709
     * 电信：133,1349,153,180,189,1700
     */
    //    NSString * MOBILE = @"^1((3//d|5[0-35-9]|8[025-9])//d|70[059])\\d{7}$";//总况
    
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188，1705
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d|705)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186,1709
     17         */
    NSString * CU = @"^1((3[0-2]|5[256]|8[56])\\d|709)\\d{7}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189,1700
     22         */
    NSString * CT = @"^1((33|53|8[09])\\d|349|700)\\d{7}$";
    
    
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    
    NSPredicate *regexTestCM = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regexTestCU = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regexTestCT = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    NSPredicate *regexTestPHS;
    BOOL result;
    
    if (mobileOnly == NO) {
        NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
        regexTestPHS = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",PHS];
        result = ([regexTestCM evaluateWithObject:self] == YES) || ([regexTestCT evaluateWithObject:self] == YES)
        || ([regexTestCU evaluateWithObject:self] == YES) || ([regexTestPHS evaluateWithObject:self] == YES);
    } else {
        result = ([regexTestCM evaluateWithObject:self] == YES) || ([regexTestCT evaluateWithObject:self] == YES)
        || ([regexTestCU evaluateWithObject:self] == YES);
    }
    
    if (result == YES) {
        return YES;
    } else {
        return NO;
    }
}

- (BOOL)validateIdentityCard{
    if (self.length != 18) {
        return NO;
    }
    
    NSArray *codeArray = @[@"7",@"9",@"10",@"5",@"8",@"4",@"2",@"1",
                           @"6",@"3",@"7",@"9",@"10",@"5",@"8",@"4",@"2"];
    NSDictionary *checkCodeDic =
    [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
                                         @"1",@"0",@"X",@"9",@"8",@"7",@"6",@"5",@"4",@"3",@"2", nil]
                                forKeys:[NSArray arrayWithObjects:
                                         @"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil]];
    NSScanner* scan = [NSScanner scannerWithString:[self substringToIndex:17]];
    int val;
    BOOL isNum = [scan scanInt:&val] && [scan isAtEnd];
    
    if (!isNum) {
        return NO;
    }
    
    int sumValue = 0;
    
    for (int i =0; i<17; i++) {
        sumValue += [[self substringWithRange:NSMakeRange(i , 1)] intValue] * [[codeArray objectAtIndex:i] intValue];
    }
    
    NSString *strlast = [checkCodeDic objectForKey:[NSString stringWithFormat:@"%d", sumValue%11]];
    
    if ([strlast isEqualToString: [[self substringWithRange:NSMakeRange(17, 1)] uppercaseString]]) {
        return YES;
    }
    
    return  NO;
}

- (BOOL)validateEmail{
    NSString *emailCheck = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailCheck];
    return [emailTest evaluateWithObject:self];
}

- (BOOL)validateUserName{
    NSString * regex = @"^[A-Za-z0-9]{4,15}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:self];
    return isMatch;
}

+ (NSString *)validateEmptyString:(NSString *)str{
    if (str == nil) {
        return @"";
    }else{
        return str;
    }
}

@end
